package be.jaisy.ui.gwt.client.widget.table;

import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Widget;

public class Table extends SortableTable {

	private String[] headers;
	private Widget[] filters;

	public Table(String[] headers) {
		super();
		this.headers = headers;
		prepareTable();
		prepareHeaders();
	}
	
	public Table(String[] headers, Widget[] widgets){
		this(headers);
		prepareFilters();
	}

	protected void prepareTable() {
		this.setWidth(100 + "%");
		this.setStyleName("sortableTable");
		this.setBorderWidth(1);
		this.setCellPadding(4);
		this.setCellSpacing(1);
	}

	protected void prepareHeaders() {

		for (int i = 0; i < headers.length; i++) {
			this.addColumnHeader(headers[i], i);
		}
		// Set Style Name for the header
		getRowFormatter().setStyleName(0, "tableHeader");
		// Set the styles for the headers
		for (int colIndex = 0; colIndex < headers.length; colIndex++) {
			getCellFormatter().setStyleName(0, colIndex, "headerStyle");
			getCellFormatter().setAlignment(0, colIndex, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		}
	}

	protected void prepareFilters() {

		for (int i = 0; i < filters.length; i++) {
			if(this.filters[i]!=null)
				this.setWidget(0, i, this.filters[i]);
		}
	}
	
	
//	public void drawTable() {
//
//		for (int rowIndex = 0; rowIndex < getRowCount(); rowIndex++) {
//
//			if (rowIndex % 2 == 0) {
//				getRowFormatter().setStyleName(rowIndex, "customRowStyle");
//			} else {
//				getRowFormatter().setStyleName(rowIndex, "tableRow");
//			}
//			for (int colIndex = 0; colIndex < 6; colIndex++) {
//				getCellFormatter().setAlignment(rowIndex, colIndex, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);
//			}
//		}
//
//		super.drawTable();
//	}

	
	public void setValue(int rowIndex, int colIndex, Comparable value) {
		if(value == null){
			super.setValue(rowIndex, colIndex, "-");
			return;
		}
		super.setValue(rowIndex, colIndex, value);
	}

	public void clearData(){
		clear();
	}
}
